package com.cq.hd.order.convert;

import com.alibaba.fastjson.JSON;
import com.cq.hd.common.enums.biz.*;
import com.cq.hd.common.utils.NoUtils;
import com.cq.hd.member.api.vo.BusinessMerchantVo;
import com.cq.hd.member.api.vo.BusinessPointsMallSettingsVo;
import com.cq.hd.order.api.dto.ActiveEnrollInfoSonDto;
import com.cq.hd.order.api.dto.CreateOrderDto;
import com.cq.hd.order.api.dto.OrderCommissionDto;
import com.cq.hd.order.po.TbOrderItemPo;
import com.cq.hd.order.po.TbOrderPo;
import com.cq.hd.order.render.OrderContext;
import com.cq.hd.order.render.dto.ActiveGoodsDto;
import com.cq.hd.order.render.dto.AgentInfo;
import com.cq.hd.order.render.dto.GoodsPriceDetailDto;
import com.cq.hd.order.render.dto.PriceDetailDto;
import com.cq.hd.product.api.vo.ActiveVo;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author much
 * @since 2024/09/16 15:45
 */
public class OrderCreateConvert {

    public static TbOrderPo createOrderForGroupBuy(OrderContext orderContext) {
        ActiveVo activeVo = orderContext.getActiveVo();
        CreateOrderDto createOrderDto = orderContext.getCreateOrderDto();
        TbOrderPo tbOrderPo = new TbOrderPo();
        tbOrderPo.setSignInRequired(activeVo.getSignInRequired());
        tbOrderPo.setBusinessId(activeVo.getBusinessId());
        tbOrderPo.setActiveId(activeVo.getId());
        AgentInfo agentInfo = orderContext.getAgentInfo();
        tbOrderPo.setAgentId(0L);
        tbOrderPo.setAgent2Id(0L);
        if (agentInfo != null) {
            tbOrderPo.setAgentId(agentInfo.getAgent1Id() == null ? 0L : agentInfo.getAgent1Id());
            tbOrderPo.setAgent2Id(agentInfo.getAgent2Id() == null ? 0L : agentInfo.getAgent2Id());
        }
        tbOrderPo.setUserId(orderContext.getUserId());
        tbOrderPo.setActiveShareCode(createOrderDto.getCode());
        tbOrderPo.setOrderNo(NoUtils.getActiveOrderNo());
        tbOrderPo.setOrderType(activeVo.getActiveType());
        tbOrderPo.setPromotionType(PromotionTypeEnum.GROUP_BUY.name());

        tbOrderPo.setActiveTitle(activeVo.getActiveTitle());
        tbOrderPo.setActiveImg(activeVo.getActiveImg());
        tbOrderPo.setStartTime(activeVo.getStartTime());
        tbOrderPo.setEndTime(activeVo.getEndTime());
        tbOrderPo.setValidStartTime(activeVo.getValidStartTime());
        tbOrderPo.setValidEndTime(activeVo.getValidEndTime());
        tbOrderPo.setSubsidyState(activeVo.getSubsidyState());
        tbOrderPo.setDiscountState(activeVo.getDiscountState());
        tbOrderPo.setStepPriceState(activeVo.getStepPriceState());
        tbOrderPo.setAgentState(activeVo.getAgentState());
        tbOrderPo.setVerifyMethod(activeVo.getVerifyMethod());
        tbOrderPo.setTotalStock(activeVo.getTotalStock());
        tbOrderPo.setOrderState(OrderStateEnum.WAIT_PAY.getValue());
        tbOrderPo.setZeroState(activeVo.getZeroState());
        tbOrderPo.setOrderNum(createOrderDto.getOrderNum());
        tbOrderPo.setActivePrice(activeVo.getActivePrice());
        tbOrderPo.setNoFullAutoRefundState(activeVo.getNoFullAutoRefundState());
        tbOrderPo.setRefundableExpiredUnused(activeVo.getRefundableExpiredUnused());
        tbOrderPo.setSignInRequired(activeVo.getSignInRequired());

        PriceDetailDto priceDetailDto = orderContext.getPriceDetailDTO();
        tbOrderPo.setOrderPrice(priceDetailDto.getOrderPrice());
        tbOrderPo.setPayPrice(priceDetailDto.getPayPrice());
        tbOrderPo.setRedPacketBillId(createOrderDto.getRedPacketBillId());
        tbOrderPo.setRedPacketAmt(priceDetailDto.getRedPacketAmt());
        tbOrderPo.setDiscountAmt(priceDetailDto.getDiscountAmt());
        tbOrderPo.setOrderIncomeAmt(priceDetailDto.getOrderIncomeAmt());

        // 佣金/积分
        OrderCommissionDto commissionDto = orderContext.getCommissionDto();
        tbOrderPo.setCommissionAmt(activeVo.getCommissionAmt());
        tbOrderPo.setCommissionRate(activeVo.getCommissionRate());
        tbOrderPo.setOtherEquity(commissionDto.getOtherEquity());
        tbOrderPo.setExclusiveCommissionRate(commissionDto.getExclusiveCommissionRate());
        tbOrderPo.setSubAgentCommissionAmt(commissionDto.getSubAgentCommissionAmt());
        tbOrderPo.setCalCommissionType(commissionDto.getCalCommissionType());
        tbOrderPo.setActualCommissionAmt(commissionDto.getActualCommissionAmt());
        tbOrderPo.setAgentPointsAmt(commissionDto.getAgentPointsAmt());
        tbOrderPo.setSubAgentPointsAmt(commissionDto.getSubAgentPointsAmt());

        // 用户积分
        BusinessPointsMallSettingsVo businessPointsMallSettingsVo = orderContext.getBusinessPointsMallSettingsVo();
        BigDecimal consumePointsRate = businessPointsMallSettingsVo.getConsume(); // 每消费1元，奖励x积分
        BigDecimal userPointsAmt = priceDetailDto.getPayPrice().multiply(consumePointsRate);
        tbOrderPo.setUserPointsAmt(userPointsAmt);

        LocalDateTime now = LocalDateTime.now();
        tbOrderPo.setOrderTime(now);
        tbOrderPo.setCreateTime(now);
        tbOrderPo.setUpdateTime(now);

        return tbOrderPo;
    }

    public static TbOrderItemPo createOrderItem(TbOrderPo tbOrderPo, ActiveGoodsDto activeGoodsDto) {
        TbOrderItemPo tbOrderItemPo = new TbOrderItemPo();
        tbOrderItemPo.setBusinessId(tbOrderPo.getBusinessId());
        tbOrderItemPo.setActiveId(tbOrderPo.getActiveId());
        tbOrderItemPo.setAgentId(tbOrderPo.getAgentId());
        tbOrderItemPo.setAgent2Id(tbOrderPo.getAgent2Id());
        tbOrderItemPo.setOrderId(tbOrderPo.getId());
        tbOrderItemPo.setUserId(tbOrderPo.getUserId());
        tbOrderItemPo.setActiveGoodsId(activeGoodsDto.getId());
        tbOrderItemPo.setSubOrderNo(NoUtils.getActiveSubOrderNo());
        tbOrderItemPo.setOrderType(tbOrderPo.getOrderType());
        // 团购订单所属商户，商品所属商户
        tbOrderItemPo.setMerchantId(activeGoodsDto.getMerchantId());
        tbOrderItemPo.setGoodsId(activeGoodsDto.getGoodsId());
        tbOrderItemPo.setMulState(activeGoodsDto.getMulState());
        tbOrderItemPo.setGoodsName(activeGoodsDto.getGoodsName());
        tbOrderItemPo.setGoodsImgs(activeGoodsDto.getGoodsImgs());
        tbOrderItemPo.setGoodsPrice(activeGoodsDto.getGoodsPrice());
        tbOrderItemPo.setGoodsNum(1);

        GoodsPriceDetailDto goodsPriceDetailDto = activeGoodsDto.getGoodsPriceDetailDto();
        tbOrderItemPo.setMerchantAmt(goodsPriceDetailDto.getMerchantAmt());
        tbOrderItemPo.setMerchantSubsidyAmt(goodsPriceDetailDto.getMerchantSubsidyAmt());
        tbOrderItemPo.setCommissionAmt(BigDecimal.ZERO);
        tbOrderItemPo.setSubAgentCommissionAmt(BigDecimal.ZERO);
        tbOrderItemPo.setRedPacketAmt(goodsPriceDetailDto.getRedPacketAmt());
        tbOrderItemPo.setTotalSubsidyAmt(goodsPriceDetailDto.getTotalSubsidyAmt());
        tbOrderItemPo.setDistributionOrderAmt(goodsPriceDetailDto.getDistributionOrderAmt());
        tbOrderItemPo.setReturnAmt(BigDecimal.ZERO);
        tbOrderItemPo.setSubOrderState(SubOrderStateEnum.WAIT_PAY.getValue());
        tbOrderItemPo.setSettleState(SettleStateEnum.NO.getValue());
        tbOrderItemPo.setOrderTime(tbOrderPo.getOrderTime());
        tbOrderItemPo.setExpireTime(tbOrderPo.getValidEndTime());

        LocalDateTime now = LocalDateTime.now();
        tbOrderItemPo.setCreateTime(now);
        tbOrderItemPo.setUpdateTime(now);
        tbOrderItemPo.setUserPointsAmt(BigDecimal.ZERO);
        tbOrderItemPo.setAgentPointsAmt(BigDecimal.ZERO);
        tbOrderItemPo.setSubAgentPointsAmt(BigDecimal.ZERO);

        int pointsSettleState = 0;
        if (tbOrderPo.getUserPointsAmt().compareTo(BigDecimal.ZERO) > 0
                || tbOrderPo.getAgentPointsAmt().compareTo(BigDecimal.ZERO) > 0
                || tbOrderPo.getSubAgentCommissionAmt().compareTo(BigDecimal.ZERO) > 0) {
            pointsSettleState = 1;
        }
        tbOrderItemPo.setPointsSettleState(pointsSettleState);

        tbOrderItemPo.setVerifyType(activeGoodsDto.getVerifyType());
        tbOrderItemPo.setPftSupplier(activeGoodsDto.getPftSupplier());
        tbOrderItemPo.setPftProduct(activeGoodsDto.getPftProduct());
        tbOrderItemPo.setPftTicket(activeGoodsDto.getPftTicket());

        // 子订单类型
        tbOrderItemPo.setItemType(activeGoodsDto.getType());

        return tbOrderItemPo;
    }

}
